import { Application } from 'egg';
import { DataTypes, Model } from 'sequelize';

export interface ExclusiveDishAttributes {
  id?: number;
  restaurant_id: number;
  category_id: number;
  name: string;
  pictures?: string; // 逗号分隔的图片路径
  price?: number;
  description?: string;
  is_hide?: number;
  created_at?: Date;
  updated_at?: Date;
}

export interface ExclusiveDishInstance extends Model<ExclusiveDishAttributes>, ExclusiveDishAttributes {}

export default (app: Application) => {
  const { INTEGER, STRING, DECIMAL, TEXT, DATE } = DataTypes;

  const ExclusiveDish = app.model.define<ExclusiveDishInstance>('ExclusiveDish', {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    restaurant_id: { type: INTEGER, allowNull: false, comment: '专属餐厅ID' },
    category_id: { type: INTEGER, allowNull: false, comment: '分类ID' },
    name: { type: STRING(100), allowNull: false, comment: '菜品名称' },
    pictures: { type: STRING(500), allowNull: true, comment: '菜品图片(逗号分隔)' },
    price: { type: DECIMAL(10, 2), allowNull: true, comment: '价格' },
    description: { type: TEXT, allowNull: true, comment: '菜品描述' },
    is_hide: { type: INTEGER, allowNull: true, defaultValue: 0, comment: '是否隐藏' },
    created_at: { type: DATE, allowNull: false, defaultValue: DataTypes.NOW },
    updated_at: { type: DATE, allowNull: false, defaultValue: DataTypes.NOW },
  }, {
    tableName: 'exclusive_dishes',
    comment: '专属餐厅菜品表',
  });

  (ExclusiveDish as any).associate = () => {
    app.model.ExclusiveDish.belongsTo(app.model.ExclusiveRestaurant, { foreignKey: 'restaurant_id', as: 'restaurant' });
    app.model.ExclusiveDish.belongsTo(app.model.ExclusiveDishCategory, { foreignKey: 'category_id', as: 'category' });
  };

  return ExclusiveDish;
};